﻿using System.Security.Policy;
using GoKuai_EntSDK.UtilClass;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GoKuai_EntSDK.Data
{
    public class Condition
    {
        public Condition()
        {
            Limit = new[] {0, 1000};
        }

        public enum ConditionType
        {
            EQ,
            GT,
            IT,
            IN,
            BETWEEN,
            TEXT,
            LIKE,
            PREFIX
        }

        public object Order { set; get; }

        /// <summary>
        /// 排序方法
        /// </summary>
        public object OrderMethod { private get; set; }

        /// <summary>
        /// 排序类型
        /// </summary>
        public object OrderType { private get; set; }

        /// <summary>
        /// 分页，长度为2的数组，例 [0，100]
        /// </summary>
        public int[] Limit { set; get; }

        /// <summary>
        /// 搜索条件
        /// </summary>
        public Include Include { set; get; }

        /// <summary>
        /// 过滤条件
        /// </summary>
        public Exclude Exclude { set; get; }

        /// <summary>
        /// 将condition对象转化为json对象
        /// </summary>
        /// <returns></returns>
        public string ToJsonString()
        {
            if (OrderMethod != null && OrderType != null)
            {

                Order = new object[] { new ConditionSortParam((OrderType)OrderType,(OrderMethod)OrderMethod) };
                OrderType = null;
                OrderMethod = null;
                
            }
            return SimpleJson.SerializeObject(this).ToLower();
        }

        /// <summary>
        /// 两者之间['between', [paramArr[0],paramArr[1]]]
        /// </summary>
        /// <param name="paramArr"></param>
        /// <returns></returns>
        public static object[] BetweenParam(object[] paramArr)
        {
            return Convert(ConditionType.BETWEEN, paramArr);
        }

        /// <summary>
        /// 大于['eq', param]
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static object[] EqParam(object param)
        {
            return Convert(ConditionType.EQ, param.ToString());
        }

        /// <summary>
        /// 模糊查询['like', param]
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static object[] LikeParam(object param)
        {
            return Convert(ConditionType.LIKE, param.ToString());
        }


        /// <summary>
        /// 大于['gt', param]
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static object[] GtParam(object param)
        {
            return Convert(ConditionType.GT, param.ToString());
        }

        /// <summary>
        ///IN 查询 ['in', [paramArr[0]，paramArr[1]]] long arr
        /// </summary>
        /// <param name="paramArr"></param>
        /// <returns></returns>
        public static object[] InParam(long[] paramArr)
        {
            return Convert(ConditionType.IN, paramArr);
        }

        /// <summary>
        ///IN 查询 ['in', [paramArr[0]，paramArr[1]]] string arr
        /// </summary>
        /// <param name="paramArr"></param>
        /// <returns></returns>
        public static object[] InParam(object[] paramArr)
        {
            return Convert(ConditionType.IN, paramArr);
        }

        /// <summary>
        /// 小于['it', param]
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static object[] ItParam(long param)
        {
            return Convert(ConditionType.IT, param.ToString());
        }


        /// <summary>
        /// 全文查询['text', param]
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static object[] TextParam(object param)
        {
            return Convert(ConditionType.TEXT, param.ToString());
        }


        /// <summary>
        ///从头匹配 ['prefix', param]
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static object[] PrefixParam(object param)
        {
            return Convert(ConditionType.PREFIX, param.ToString());
        }

        private static object[] Convert(ConditionType type, object param)
        {
            var convertedParam = new object[2];
            switch (type)
            {
                case ConditionType.TEXT:
                case ConditionType.PREFIX:
                case ConditionType.LIKE:

                    convertedParam[0] = type.ToString();
                    convertedParam[1] = param.ToString();
                    break;
                default:
                    convertedParam[0] = type.ToString();
                    convertedParam[1] = param.ToString();
                    break;
            }
            return convertedParam;
        }


        private static object[] Convert(ConditionType type, object[] arr)
        {
            object[] convertedParam = {type.ToString(), arr};

            return convertedParam;
        }
    }

    public class Include
    {
        public object[] Path { set; get; }
        public object[] Keywords { set; get; }
        public object[] Creator { set; get; }
        public object[] Dateline { set; get; }
        public object[] Filesize { set; get; }
    }

    public class Exclude
    {
        public object[] Creator { set; get; }
        public object[] Modifier { set; get; }
        public object[] Keywords { set; get; }
        public object[] Extension { set; get; }
    }
}